/**
 * 
 */
package dbAccess;

import java.sql.*;
import java.util.GregorianCalendar;

import exceptions.*;

import model.Lot;

/**
 * @author Anthares & Hennek
 *
 */
public class LotDBAccess {
	public Lot getLot(int num_lot) throws PasIdentificationException, DatabaseErrorException{
		Connection conn = DBConnexionSingleton.getInstance();
		ImprimerieDBAccess idba = new ImprimerieDBAccess();
		PreparedStatement stat;
		Lot lot = null;
		String query = "Select * from lot where num_lot = ?;";
		try {
			stat = conn.prepareStatement(query);
				stat.setInt(1, num_lot);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				java.sql.Date date_crea = data.getDate("date_creation");
				GregorianCalendar gc = new GregorianCalendar();
				gc.setTime(date_crea);
				
				lot = new Lot(data.getInt("num_lot"));
				lot.setDate_creation(gc);
				lot.setImprimeur(idba.getImprimerie(data.getInt("fournisseur_id")));
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return lot;
	}
	public Lot getLastLot() throws PasIdentificationException, DatabaseErrorException{
		Connection conn = DBConnexionSingleton.getInstance();
		ImprimerieDBAccess idba = new ImprimerieDBAccess();
		PreparedStatement stat;
		Lot lot = null;
		String query = "Select * from lot where num_lot = (select max(num_lot) from lot);";
		try {
			stat = conn.prepareStatement(query);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				java.sql.Date date_crea = data.getDate("date_creation");
				GregorianCalendar gc = new GregorianCalendar();
				gc.setTime(date_crea);
				
				lot = new Lot(data.getInt("num_lot"));
				lot.setDate_creation(gc);
				lot.setImprimeur(idba.getImprimerie(data.getInt("fournisseur_id")));
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return lot;
	}
}
